package hlhj.fhp.supreme.activitys

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.MessageListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_option_aty.*
import java.util.ArrayList

class SystemMessageAty : BaseAty() {
    private val datas=ArrayList<MessageListBean.DataBean>()
    private lateinit var adp:BaseQuickAdapter<MessageListBean.DataBean,BaseViewHolder>
    override fun getContentId(): Int = R.layout.activity_system_message_aty


    override fun initView() {
        adp=object :BaseQuickAdapter<MessageListBean.DataBean,BaseViewHolder>(R.layout.system_msg_item,datas){
            override fun convert(helper: BaseViewHolder?, item: MessageListBean.DataBean?) {
                helper?.setVisible(R.id.imgIsRead,when(item?.status){
                    0->true
                    else->false
                })
                helper?.setText(R.id.itemTittle,item?.title)
                helper?.setText(R.id.itemInfo,item?.des)
                helper?.setText(R.id.itemTime,MyUtils.timeStampToStr(item?.create_time!!.toLong()))
            }
        }
        recyclerview.adapter=adp
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)
        getData()
    }

    private fun getData() {
        OkGo.post<MessageListBean>(Urls.BaseUrl+Urls.MESSAGE_LIST)
                .execute(object :JsonCallBack<MessageListBean>(this){
                    override fun onSuccess(response: Response<MessageListBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            datas.addAll(bean?.data)
                            adp.notifyDataSetChanged()
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
        OkGo.post<BaseBean>(Urls.BaseUrl+Urls.READ_ALL)
                .execute(object :JsonCallBack<BaseBean>(this@SystemMessageAty){
                    override fun onSuccess(response: Response<BaseBean>?) {

                    }
                })
    }

    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
    }
}
